package com.joolun.web.controller.script;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joolun.common.core.domain.R;
import com.joolun.script.entity.Script;
import com.joolun.script.service.ScriptService;
import com.joolun.web.vo.RecommendDto;
import com.joolun.web.vo.ScriptVO;
import com.joolun.weixin.utils.ThirdSessionHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 剧本
 *
 * @author 臣服
 * @date 2022-01-12 10:52:16
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/script")
@Api(value = "script", tags = "剧本管理")
public class ScriptController {

    private final ScriptService scriptService;

    /**
     * 分页列表
     *
     * @param page   分页对象
     * @param script 剧本
     * @return
     */
    @ApiOperation(value = "分页列表")
    @GetMapping("/page")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(paramType = "query", dataType = "com.baomidou.mybatisplus.extension.plugins.pagination.Page"),
            @ApiImplicitParam(paramType = "query", dataType = "com.joolun.script.entity.Script"),
            @ApiImplicitParam(paramType = "query", required = true, name = "ascs", value = "升序字段参数 ascs | descs 任选其一或者混用都可，但是要注意顺序会影响排序结果", dataType = "String"),
            @ApiImplicitParam(paramType = "query", required = true, name = "descs", value = "降序字段参数 ascs | descs 任选其一或者混用都可，但是要注意顺序会影响排序结果", dataType = "String")
    }
    )
    public R<Page<Script>> getPage(Page page, Script script) {
        return R.ok(scriptService.page(page, Wrappers.query(script)));
    }

    /**
     * 剧本查询
     *
     * @param id
     * @return R
     */
    @ApiOperation(value = "剧本查询")
    @GetMapping("/{id}")
    public R<ScriptVO> getById(@PathVariable("id") String id) {
        Script script = scriptService.getById(id);
        return R.ok(ScriptVO.toScriptVO(script));
    }

    /**
     * 剧本新增
     *
     * @param vo 剧本
     * @return R
     */
    @ApiOperation(value = "剧本新增")
    @PostMapping
    public R<Boolean> save(@RequestBody ScriptVO vo) {
        Boolean isAdmin = ThirdSessionHolder.getThirdSession().getIsAdmin();
        if (!isAdmin) {
            return R.failed("没有权限");
        }
        Script script = vo.toScript();
        return R.ok(scriptService.save(script));
    }

    /**
     * 剧本修改
     *
     * @param vo 剧本
     * @return R
     */
    @ApiOperation(value = "剧本修改")
    @PutMapping
    public R<Boolean> updateById(@RequestBody ScriptVO vo) {
        Boolean isAdmin = ThirdSessionHolder.getThirdSession().getIsAdmin();
        if (!isAdmin) {
            return R.failed("没有权限");
        }
        Script script = vo.toScript();
        LambdaQueryWrapper<Script> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Script::getId, vo.getId());
        if (StringUtils.isBlank(script.getRoles())) {
            queryWrapper.isNull(Script::getRoles);
        }
        return R.ok(scriptService.update(script, queryWrapper));
    }


    /**
     * 剧本推荐|取消推荐
     *
     * @param dto 剧本
     * @return R
     */
    @ApiOperation(value = "剧本推荐|取消推荐")
    @PutMapping("/recommend")
    public R<Boolean> recommend(@RequestBody RecommendDto dto) {
        Boolean isAdmin = ThirdSessionHolder.getThirdSession().getIsAdmin();
        if (!isAdmin) {
            return R.failed("没有权限");
        }
        Script script = new Script();
        script.setId(dto.getScriptId());
        script.setRecommend(dto.getStatus());
        script.setRecommendTime(LocalDateTime.now());
        return R.ok(scriptService.updateById(script));
    }

    /**
     * 剧本删除
     *
     * @param id
     * @return R
     */
    @ApiOperation(value = "剧本删除")
    @DeleteMapping("/{id}")
    public R<Boolean> removeById(@PathVariable String id) {
        Boolean isAdmin = ThirdSessionHolder.getThirdSession().getIsAdmin();
        if (!isAdmin) {
            return R.failed("没有权限");
        }
        return R.ok(scriptService.removeById(id));
    }

}
